package com.github.niefy.common.utils;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Component
@ConfigurationProperties(prefix = "douyin.changdu")
@Getter
@Setter
public class ChangDuCall {

    private String distributorId;
    private String secretKey;

    public String generateSign() {
        long timestamp = System.currentTimeMillis() / 1000;
        String toBeHashed = distributorId + secretKey + timestamp;
        return Objects.requireNonNull(MD5Util.getMd5(toBeHashed)).toLowerCase();
    }

    /**
     * 获取用户信息接口
     */
    public String getUserListV1() {
        long timestamp = System.currentTimeMillis() / 1000;
        String url = "https://www.changdunovel.com/novelsale/openapi/user/list/v1/";
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("distributor_id", distributorId);
        paramMap.put("ts", String.valueOf(timestamp));

        paramMap.put("sign", generateSign());
        // 页码，从0开始
        paramMap.put("page_index", "0");
        // 如果这里没改，那就是没付尾款 18000855903
        // 每页大小 默认10 最大 1000
        paramMap.put("page_size", "1000");
        // 获取充值用户数据
        paramMap.put("show_not_recharge", "true");

        String withParams = RestTemplateUtils.getWithParams(url, String.class, paramMap);
        System.out.println("getUserListV1 = " + withParams);

        return withParams;
    }

    /**
     * 获取用户充值事件
     * <p>
     * 每次充值就是一个订单，从这里面，可以取到每次充值的记录
     */
    public String getUserReChargeV1(int offset) {
        long timestamp = System.currentTimeMillis() / 1000;
        String url = "https://www.changdunovel.com/novelsale/openapi/user/recharge/v1";
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("distributor_id", distributorId);
        paramMap.put("ts", String.valueOf(timestamp));
        paramMap.put("sign", generateSign());

        paramMap.put("paid", "true");

        // long startTimeLong = startTime.toInstant(ZoneOffset.of("+08:00")).toEpochMilli();
        // long endTimeLong = endTime.toInstant(ZoneOffset.of("+08:00")).toEpochMilli();

        // paramMap.put("begin", String.valueOf(startTimeLong));
        // paramMap.put("end", String.valueOf(endTimeLong));
        // paramMap.put("limit", "100");
        paramMap.put("offset", String.valueOf(offset));

        String withParams = RestTemplateUtils.getWithParams(url, String.class, paramMap);
        System.out.println("getUserReChargeV1 = " + withParams);

        return withParams;
    }

    /**
     * 获取用户消费记录
     */
    @Deprecated
    public String getUserPurchaseV1() {
        long timestamp = System.currentTimeMillis() / 1000;
        String url = "https://www.changdunovel.com/novelsale/openapi/user/purchase/v1/";
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("distributor_id", distributorId);
        paramMap.put("ts", String.valueOf(timestamp));
        paramMap.put("sign", generateSign());
        // 用户设备id
        paramMap.put("device_id", "de7cedfa76ba426717d23fb296ff9941");

        String withParams = RestTemplateUtils.getWithParams(url, String.class, paramMap);
        System.out.println("getUserPurchaseV1 = " + withParams);

        return withParams;
    }

    /**
     * 获取书籍信息
     */
    public String getContentBookMetaV1() {
        long timestamp = System.currentTimeMillis() / 1000;
        String url = "https://www.changdunovel.com/novelsale/openapi/content/book_meta/v1/";
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("distributor_id", distributorId);
        paramMap.put("ts", String.valueOf(timestamp));
        paramMap.put("sign", generateSign());
        paramMap.put("book_id", "");

        String withParams = RestTemplateUtils.getWithParams(url, String.class, paramMap);
        System.out.println("getUserReChargeV1 = " + withParams);


        return withParams;
    }


}
